const zoomIn = (animation, item) => {
  var duration = item.se.duration
  var step = { duration: duration / 2, delay: 0 }
  var scale = 1.0
  var showScale = 1.2
  var hideScale = 1.4
  animation.scale(scale, scale).step()
  animation.opacity(1).scale(showScale, showScale).step(step)
  animation.opacity(0).scale(hideScale, hideScale).step(step)
}

const zoomOut = (animation, item) => {
  var duration = item.se.duration
  var step = { duration: duration / 2, delay: 0 }
  var scale = 1.2
  var showScale = 1.1
  var hideScale = 1.0
  animation.scale(scale, scale).step()
  animation.opacity(1).scale(showScale, showScale).step(step)
  animation.opacity(0).scale(hideScale, hideScale).step(step)
}

const moveLeft = (animation, item) => {
  var duration = item.se.duration
  var step = { duration: duration / 2, delay: 0 }
  var scale = 1.2
  animation.scale(scale, scale).step()
  animation.opacity(1).translateX(-20).step(step)
  animation.opacity(0).translateX(-40).step(step)
}

const moveRight = (animation, item) => {
  var duration = item.se.duration
  var step = { duration: duration / 2, delay: 0 }
  var scale = 1.2
  animation.scale(scale, scale).step()
  animation.opacity(1).translateX(20).step(step)
  animation.opacity(0).translateX(40).step(step)
}

const moveUp = (animation, item) => {
  var duration = item.se.duration
  var step = { duration: duration / 2, delay: 0 }
  var scale = 1.2
  animation.scale(scale, scale).step()
  animation.opacity(1).translateY(-20).step(step)
  animation.opacity(0).translateY(-40).step(step)
}

const moveDowm = (animation, item) => {
  var duration = item.se.duration
  var step = { duration: duration / 2, delay: 0 }
  var scale = 1.2
  animation.scale(scale, scale).step()
  animation.opacity(1).translateY(20).step(step)
  animation.opacity(0).translateY(40).step(step)
}

const animations = {
  zoomIn: zoomIn,
  zoomOut: zoomOut,
  moveLeft: moveLeft,
  moveUp: moveUp,
  moveRight: moveRight,
  moveDown: moveDowm
}

const avatarAnim = duration => {
  var anim = wx.createAnimation({
    timingFunction: 'ease-out',
    delay: duration + 200
  })
  anim.opacity(1).translateY(-40).step()
  return anim.export()
}

const nicknameAnim = duration => {
  var anim = wx.createAnimation({
    timingFunction: 'ease-out',
    delay: duration + 500
  })
  anim.opacity(1).translateY(-40).step()
  return anim.export()
}

const replayAnim = duration => {
  var anim = wx.createAnimation({
    timingFunction: 'ease-out',
    delay: duration + 800
  })
  anim.opacity(1).translateY(-40).step()
  return anim.export()
}

const bgAnim = duration => {
  var anim = wx.createAnimation({
    timingFunction: 'ease-out',
    delay: duration
  })
  anim.opacity(1).step()
  return anim.export()
}

const parseAnim = datas => {
  var anims = []
  var delay = 0
  for (var i = 0; i < datas.length; i++) {
    var item = datas[i]
    var animation = wx.createAnimation({
      timingFunction: 'linear',
      delay: delay
    })
    var animationType = item.se.animation
    animations[animationType](animation, item)

    var data = {
      anim: animation.export(),
      image: item.url
    }

    anims.push(data)

    delay += Number(item.se.duration)
  }

  return {
     datas: anims.reverse(),
     avatarAnim: avatarAnim(delay),
     nicknameAnim: nicknameAnim(delay),
     replayAnim: replayAnim(delay),
     bgAnim: bgAnim(delay),
     duration: delay
  }
}

module.exports = {
  parseAnim: parseAnim
}